package com.cuit.qx.controller;


import cn.hutool.http.HttpResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cuit.qx.bean.Warning;
import com.cuit.qx.entity.Checkout;
import com.cuit.qx.entity.CorrectForecast;
import com.cuit.qx.service.CheckoutService;
import com.cuit.qx.utils.ReturnData;
import com.cuit.qx.utils.ReturnStateCode;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcj
 * @since 2021-11-25
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/checkout")
public class CheckoutController {

    @Autowired
    private CheckoutService service;

    private static final Logger LOG = LoggerFactory.getLogger(CheckoutController.class);
    /**
     * @description:  type：0在线检验 1离线检验
      * @return:
     * @author: wjd
     * @time: 2021/11/25 11:08
     */
    @RequestMapping("/getCheckout")
    public ReturnData getCheckout(@RequestBody Map<String,String> map){

        LambdaQueryWrapper<Checkout> wrapper =new LambdaQueryWrapper();
        wrapper.eq(Checkout::getCheckoutType,map.get("type"));//0代表在线  1代表离线
        wrapper.ge(Checkout::getCheckoutDate,map.get("startTime")); //查询开始时间
        wrapper.le(Checkout::getCheckoutDate,map.get("endTime"));//查询结束时间
        wrapper.eq(Checkout::getCheckoutStaion,map.get("station"));//查询站点名称
        wrapper.eq(Checkout::getCheckoutTime,map.get("checoutTime"));//检验起报时间  08、20
        wrapper.eq(Checkout::getTimeliness,map.get("timeLiness"));//检验累计雨量 12h
        wrapper.eq(Checkout::getOrders,map.get("order"));//检验阈值 0.1、1.0
        wrapper.eq(Checkout::getCheckoutMethod,map.get("methond"));//检验方法
        //检验方法 0代表TS评分 1代表ETS 2代表空报 3代表漏报 4代表预报偏差 5代表命中率
        List<Checkout> list = service.list(wrapper);

        if (list.size()>0){
            return  new ReturnData(ReturnStateCode.SUCCEE.getCode(),ReturnStateCode.SUCCEE.getMsg(),list);
        }
        return  new ReturnData(ReturnStateCode.SUCCEEBUT.getCode(),ReturnStateCode.SUCCEEBUT.getMsg());
    }


    /**
     * @description: 下载检验数据
     * 检验方法： 0代表TS评分 1代表ETS 2代表空报 3代表漏报 4代表预报偏差 5代表命中率
     * 模式： 0代表 grepse  1 代表华南模式  2代表EC模式 3代表订正模式
     * @return:
     * @author: wjd
     * @time: 2021/11/25 14:49
     */
    @RequestMapping(value = "/exportXlChecoutData", method = RequestMethod.POST)
    public ModelAndView exportXlChecoutData(HttpServletResponse response, @RequestBody Map<String, String> map) throws UnsupportedEncodingException {

        LambdaQueryWrapper<Checkout> wrapper =new LambdaQueryWrapper();
        wrapper.eq(Checkout::getCheckoutType,map.get("type"));//0代表在线  1代表离线
        wrapper.ge(Checkout::getCheckoutDate,map.get("startTime")); //查询开始时间
        wrapper.le(Checkout::getCheckoutDate,map.get("endTime"));//查询结束时间
        wrapper.eq(Checkout::getCheckoutStaion,map.get("station"));//查询站点名称
        wrapper.eq(Checkout::getCheckoutTime,map.get("checoutTime"));//检验起报时间  08、20
        wrapper.eq(Checkout::getTimeliness,map.get("timeLiness"));//检验累计雨量 12h
        wrapper.eq(Checkout::getOrders,map.get("order"));//检验阈值 0.1、1.0
        wrapper.eq(Checkout::getCheckoutMethod,map.get("methond"));//检验方法

        List<Checkout> list = service.list(wrapper);
        ArrayList<Checkout> objects = new ArrayList<>(); //用来组装数据

        list.forEach(
                item->{
                    String pattern = pattern(item.getPattern());//模式
                    String s = checkouMethod(item.getCheckoutMethod());//检验方法
                    item.setPattern(pattern);
                    item.setCheckoutMethod(s);
                    objects.add(item);
                }
        );

        /**组装数据**/
        String fileName= map.get("station")+"站"+map.get("startTime")+"至"+map.get("endTime")+map.get("checoutTime")+"时"
                + checkouMethod(map.get("methond"))+"检验数据";
        String tile=map.get("station")+"逐"+map.get("timeLiness")+"降水预报"+checkouMethod(map.get("methond"))+"检验结果";

        String selectDate= map.get("startTime")+"至"+map.get("endTime");

        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename = "+new String(fileName.getBytes(),"UTF-8"));

        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        // 导出文件名称
        try {
            mv.addObject(NormalExcelConstants.FILE_NAME,  new String(fileName.getBytes(),"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            LOG.error("导出检验数据异常："+e);
            e.printStackTrace();
        }
        mv.addObject(NormalExcelConstants.CLASS, Checkout.class);
//        ExportParams exportParams = new ExportParams("站点检验数据", map.get("startTime")+"-"+map.get("endTime"), map.get("station"));
        ExportParams exportParams = new ExportParams(tile, "起报时间:"+selectDate, map.get("station"));
        exportParams.setImageBasePath("");
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, objects);
        return mv;
    }

    public String checkouMethod(String method){
        String methodc="";
        //检验方法 0代表TS评分 1代表ETS 2代表空报 3代表漏报 4代表预报偏差 5代表命中率
        switch (method){
            case "0":
                methodc="TS";
                break;
            case "1":
                methodc="ETS";
                break;
            case "2":
                methodc="空报";
                break;
            case "3":
                methodc="漏报";
                break;
            case "4":
                methodc="预报偏差";
                break;
            case "5":
                methodc="命中率";
                break;
        }
        return methodc;
    }

    public String pattern(String p){
        String pattern="";
        switch (p){
            case "0":
                pattern="GREPES";
                break;
            case "1":
                pattern="华南模式";
                break;
            case "2":
                pattern="EC模式";
                break;
            case "3":
                pattern="订正模式";
                break;
        }
        return pattern;
    }
}
